package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private  BaseCategory3Mapper baseCategory3Mapper;

	@Override
	public List<AlbumCategoryVo> getBaseCategoryList() {
		return baseCategoryViewMapper.getBaseCategoryList();
	}

	@Override
	public List<BaseAttribute> findAttributeByCategoryId(Long categoryId) {
		List<BaseAttribute> 	baseAttributeList =	baseAttributeMapper.findAttributeByCategoryId(categoryId);
		return baseAttributeList;
	}

	@Override
	public BaseCategoryView getBaseCategoryView(Long c3Id) {
		return baseCategoryViewMapper.selectById(c3Id);
	}

	@Override
	public AlbumCategoryVo getAllCategoryList(Long category1Id) {
		return baseCategoryViewMapper.getCategoryInfoByCategory1Id(category1Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {

		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id , category1Id);
		baseCategory2LambdaQueryWrapper.select(BaseCategory2::getId);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		List<Long> c2Ids = baseCategory2List.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());


		LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id , c2Ids);
		baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1);
		baseCategory3LambdaQueryWrapper.last("limit 7");
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);

		return baseCategory3List;
	}
}
